<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>integer.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="closure_goog_math_integer.js.html">integer.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2009 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>/**
<a name="line16"></a> * @fileoverview Defines an Integer class for representing (potentially)
<a name="line17"></a> * infinite length two&#39;s-complement integer values.
<a name="line18"></a> *
<a name="line19"></a> * For the specific case of 64-bit integers, use goog.math.Long, which is more
<a name="line20"></a> * efficient.
<a name="line21"></a> *
<a name="line22"></a> */
<a name="line23"></a>
<a name="line24"></a>goog.provide(&#39;goog.math.Integer&#39;);
<a name="line25"></a>
<a name="line26"></a>
<a name="line27"></a>
<a name="line28"></a>/**
<a name="line29"></a> * Constructs a two&#39;s-complement integer an array containing bits of the
<a name="line30"></a> * integer in 32-bit (signed) pieces, given in little-endian order (i.e.,
<a name="line31"></a> * lowest-order bits in the first piece), and the sign of -1 or 0.
<a name="line32"></a> *
<a name="line33"></a> * See the from* functions below for other convenient ways of constructing
<a name="line34"></a> * Integers.
<a name="line35"></a> *
<a name="line36"></a> * The internal representation of an integer is an array of 32-bit signed
<a name="line37"></a> * pieces, along with a sign (0 or -1) that indicates the contents of all the
<a name="line38"></a> * other 32-bit pieces out to infinity.  We use 32-bit pieces because these are
<a name="line39"></a> * the size of integers on which Javascript performs bit-operations.  For
<a name="line40"></a> * operations like addition and multiplication, we split each number into 16-bit
<a name="line41"></a> * pieces, which can easily be multiplied within Javascript&#39;s floating-point
<a name="line42"></a> * representation without overflow or change in sign.
<a name="line43"></a> *
<a name="line44"></a> * @constructor
<a name="line45"></a> * @param {Array.&lt;number&gt;} bits Array containing the bits of the number.
<a name="line46"></a> * @param {number} sign The sign of the number: -1 for negative and 0 positive.
<a name="line47"></a> */
<a name="line48"></a>goog.math.Integer = function(bits, sign) {
<a name="line49"></a>  /**
<a name="line50"></a>   * @type {!Array.&lt;number&gt;}
<a name="line51"></a>   * @private
<a name="line52"></a>   */
<a name="line53"></a>  this.bits_ = [];
<a name="line54"></a>
<a name="line55"></a>  /**
<a name="line56"></a>   * @type {number}
<a name="line57"></a>   * @private
<a name="line58"></a>   */
<a name="line59"></a>  this.sign_ = sign;
<a name="line60"></a>
<a name="line61"></a>  // Copy the 32-bit signed integer values passed in.  We prune out those at the
<a name="line62"></a>  // top that equal the sign since they are redundant.
<a name="line63"></a>  var top = true;
<a name="line64"></a>  for (var i = bits.length - 1; i &gt;= 0; i--) {
<a name="line65"></a>    var val = bits[i] | 0;
<a name="line66"></a>    if (!top || val != sign) {
<a name="line67"></a>      this.bits_[i] = val;
<a name="line68"></a>      top = false;
<a name="line69"></a>    }
<a name="line70"></a>  }
<a name="line71"></a>};
<a name="line72"></a>
<a name="line73"></a>
<a name="line74"></a>// NOTE: Common constant values ZERO, ONE, NEG_ONE, etc. are defined below the
<a name="line75"></a>// from* methods on which they depend.
<a name="line76"></a>
<a name="line77"></a>
<a name="line78"></a>/**
<a name="line79"></a> * A cache of the Integer representations of small integer values.
<a name="line80"></a> * @type {!Object}
<a name="line81"></a> * @private
<a name="line82"></a> */
<a name="line83"></a>goog.math.Integer.IntCache_ = {};
<a name="line84"></a>
<a name="line85"></a>
<a name="line86"></a>/**
<a name="line87"></a> * Returns an Integer representing the given (32-bit) integer value.
<a name="line88"></a> * @param {number} value A 32-bit integer value.
<a name="line89"></a> * @return {!goog.math.Integer} The corresponding Integer value.
<a name="line90"></a> */
<a name="line91"></a>goog.math.Integer.fromInt = function(value) {
<a name="line92"></a>  if (-128 &lt;= value &amp;&amp; value &lt; 128) {
<a name="line93"></a>    var cachedObj = goog.math.Integer.IntCache_[value];
<a name="line94"></a>    if (cachedObj) {
<a name="line95"></a>      return cachedObj;
<a name="line96"></a>    }
<a name="line97"></a>  }
<a name="line98"></a>
<a name="line99"></a>  var obj = new goog.math.Integer([value | 0], value &lt; 0 ? -1 : 0);
<a name="line100"></a>  if (-128 &lt;= value &amp;&amp; value &lt; 128) {
<a name="line101"></a>    goog.math.Integer.IntCache_[value] = obj;
<a name="line102"></a>  }
<a name="line103"></a>  return obj;
<a name="line104"></a>};
<a name="line105"></a>
<a name="line106"></a>
<a name="line107"></a>/**
<a name="line108"></a> * Returns an Integer representing the given value, provided that it is a finite
<a name="line109"></a> * number.  Otherwise, zero is returned.
<a name="line110"></a> * @param {number} value The value in question.
<a name="line111"></a> * @return {!goog.math.Integer} The corresponding Integer value.
<a name="line112"></a> */
<a name="line113"></a>goog.math.Integer.fromNumber = function(value) {
<a name="line114"></a>  if (isNaN(value) || !isFinite(value)) {
<a name="line115"></a>    return goog.math.Integer.ZERO;
<a name="line116"></a>  } else if (value &lt; 0) {
<a name="line117"></a>    return goog.math.Integer.fromNumber(-value).negate();
<a name="line118"></a>  } else {
<a name="line119"></a>    var bits = [];
<a name="line120"></a>    var pow = 1;
<a name="line121"></a>    for (var i = 0; value &gt;= pow; i++) {
<a name="line122"></a>      bits[i] = (value / pow) | 0;
<a name="line123"></a>      pow *= goog.math.Integer.TWO_PWR_32_DBL_;
<a name="line124"></a>    }
<a name="line125"></a>    return new goog.math.Integer(bits, 0);
<a name="line126"></a>  }
<a name="line127"></a>};
<a name="line128"></a>
<a name="line129"></a>
<a name="line130"></a>/**
<a name="line131"></a> * Returns a Integer representing the value that comes by concatenating the
<a name="line132"></a> * given entries, each is assumed to be 32 signed bits, given in little-endian
<a name="line133"></a> * order (lowest order bits in the lowest index), and sign-extending the highest
<a name="line134"></a> * order 32-bit value.
<a name="line135"></a> * @param {Array.&lt;number&gt;} bits The bits of the number, in 32-bit signed pieces,
<a name="line136"></a> *     in little-endian order.
<a name="line137"></a> * @return {!goog.math.Integer} The corresponding Integer value.
<a name="line138"></a> */
<a name="line139"></a>goog.math.Integer.fromBits = function(bits) {
<a name="line140"></a>  var high = bits[bits.length - 1];
<a name="line141"></a>  return new goog.math.Integer(bits, high &amp; (1 &lt;&lt; 31) ? -1 : 0);
<a name="line142"></a>};
<a name="line143"></a>
<a name="line144"></a>
<a name="line145"></a>/**
<a name="line146"></a> * Returns an Integer representation of the given string, written using the
<a name="line147"></a> * given radix.
<a name="line148"></a> * @param {string} str The textual representation of the Integer.
<a name="line149"></a> * @param {number=} opt_radix The radix in which the text is written.
<a name="line150"></a> * @return {!goog.math.Integer} The corresponding Integer value.
<a name="line151"></a> */
<a name="line152"></a>goog.math.Integer.fromString = function(str, opt_radix) {
<a name="line153"></a>  if (str.length == 0) {
<a name="line154"></a>    throw Error(&#39;number format error: empty string&#39;);
<a name="line155"></a>  }
<a name="line156"></a>
<a name="line157"></a>  var radix = opt_radix || 10;
<a name="line158"></a>  if (radix &lt; 2 || 36 &lt; radix) {
<a name="line159"></a>    throw Error(&#39;radix out of range: &#39; + radix);
<a name="line160"></a>  }
<a name="line161"></a>
<a name="line162"></a>  if (str.charAt(0) == &#39;-&#39;) {
<a name="line163"></a>    return goog.math.Integer.fromString(str.substring(1), radix).negate();
<a name="line164"></a>  } else if (str.indexOf(&#39;-&#39;) &gt;= 0) {
<a name="line165"></a>    throw Error(&#39;number format error: interior &quot;-&quot; character&#39;);
<a name="line166"></a>  }
<a name="line167"></a>
<a name="line168"></a>  // Do several (8) digits each time through the loop, so as to
<a name="line169"></a>  // minimize the calls to the very expensive emulated div.
<a name="line170"></a>  var radixToPower = goog.math.Integer.fromNumber(Math.pow(radix, 8));
<a name="line171"></a>
<a name="line172"></a>  var result = goog.math.Integer.ZERO;
<a name="line173"></a>  for (var i = 0; i &lt; str.length; i += 8) {
<a name="line174"></a>    var size = Math.min(8, str.length - i);
<a name="line175"></a>    var value = parseInt(str.substring(i, i + size), radix);
<a name="line176"></a>    if (size &lt; 8) {
<a name="line177"></a>      var power = goog.math.Integer.fromNumber(Math.pow(radix, size));
<a name="line178"></a>      result = result.multiply(power).add(goog.math.Integer.fromNumber(value));
<a name="line179"></a>    } else {
<a name="line180"></a>      result = result.multiply(radixToPower);
<a name="line181"></a>      result = result.add(goog.math.Integer.fromNumber(value));
<a name="line182"></a>    }
<a name="line183"></a>  }
<a name="line184"></a>  return result;
<a name="line185"></a>};
<a name="line186"></a>
<a name="line187"></a>
<a name="line188"></a>/**
<a name="line189"></a> * A number used repeatedly in calculations.  This must appear before the first
<a name="line190"></a> * call to the from* functions below.
<a name="line191"></a> * @type {number}
<a name="line192"></a> * @private
<a name="line193"></a> */
<a name="line194"></a>goog.math.Integer.TWO_PWR_32_DBL_ = (1 &lt;&lt; 16) * (1 &lt;&lt; 16);
<a name="line195"></a>
<a name="line196"></a>
<a name="line197"></a>/** @type {!goog.math.Integer} */
<a name="line198"></a>goog.math.Integer.ZERO = goog.math.Integer.fromInt(0);
<a name="line199"></a>
<a name="line200"></a>
<a name="line201"></a>/** @type {!goog.math.Integer} */
<a name="line202"></a>goog.math.Integer.ONE = goog.math.Integer.fromInt(1);
<a name="line203"></a>
<a name="line204"></a>
<a name="line205"></a>/**
<a name="line206"></a> * @type {!goog.math.Integer}
<a name="line207"></a> * @private
<a name="line208"></a> */
<a name="line209"></a>goog.math.Integer.TWO_PWR_24_ = goog.math.Integer.fromInt(1 &lt;&lt; 24);
<a name="line210"></a>
<a name="line211"></a>
<a name="line212"></a>/**
<a name="line213"></a> * Returns the value, assuming it is a 32-bit integer.
<a name="line214"></a> * @return {number} The corresponding int value.
<a name="line215"></a> */
<a name="line216"></a>goog.math.Integer.prototype.toInt = function() {
<a name="line217"></a>  return this.bits_.length &gt; 0 ? this.bits_[0] : this.sign_;
<a name="line218"></a>};
<a name="line219"></a>
<a name="line220"></a>
<a name="line221"></a>/** @return {number} The closest floating-point representation to this value. */
<a name="line222"></a>goog.math.Integer.prototype.toNumber = function() {
<a name="line223"></a>  if (this.isNegative()) {
<a name="line224"></a>    return -this.negate().toNumber();
<a name="line225"></a>  } else {
<a name="line226"></a>    var val = 0;
<a name="line227"></a>    var pow = 1;
<a name="line228"></a>    for (var i = 0; i &lt; this.bits_.length; i++) {
<a name="line229"></a>      val += this.getBitsUnsigned(i) * pow;
<a name="line230"></a>      pow *= goog.math.Integer.TWO_PWR_32_DBL_;
<a name="line231"></a>    }
<a name="line232"></a>    return val;
<a name="line233"></a>  }
<a name="line234"></a>};
<a name="line235"></a>
<a name="line236"></a>
<a name="line237"></a>/**
<a name="line238"></a> * @param {number=} opt_radix The radix in which the text should be written.
<a name="line239"></a> * @return {string} The textual representation of this value.
<a name="line240"></a> * @override
<a name="line241"></a> */
<a name="line242"></a>goog.math.Integer.prototype.toString = function(opt_radix) {
<a name="line243"></a>  var radix = opt_radix || 10;
<a name="line244"></a>  if (radix &lt; 2 || 36 &lt; radix) {
<a name="line245"></a>    throw Error(&#39;radix out of range: &#39; + radix);
<a name="line246"></a>  }
<a name="line247"></a>
<a name="line248"></a>  if (this.isZero()) {
<a name="line249"></a>    return &#39;0&#39;;
<a name="line250"></a>  } else if (this.isNegative()) {
<a name="line251"></a>    return &#39;-&#39; + this.negate().toString(radix);
<a name="line252"></a>  }
<a name="line253"></a>
<a name="line254"></a>  // Do several (6) digits each time through the loop, so as to
<a name="line255"></a>  // minimize the calls to the very expensive emulated div.
<a name="line256"></a>  var radixToPower = goog.math.Integer.fromNumber(Math.pow(radix, 6));
<a name="line257"></a>
<a name="line258"></a>  var rem = this;
<a name="line259"></a>  var result = &#39;&#39;;
<a name="line260"></a>  while (true) {
<a name="line261"></a>    var remDiv = rem.divide(radixToPower);
<a name="line262"></a>    var intval = rem.subtract(remDiv.multiply(radixToPower)).toInt();
<a name="line263"></a>    var digits = intval.toString(radix);
<a name="line264"></a>
<a name="line265"></a>    rem = remDiv;
<a name="line266"></a>    if (rem.isZero()) {
<a name="line267"></a>      return digits + result;
<a name="line268"></a>    } else {
<a name="line269"></a>      while (digits.length &lt; 6) {
<a name="line270"></a>        digits = &#39;0&#39; + digits;
<a name="line271"></a>      }
<a name="line272"></a>      result = &#39;&#39; + digits + result;
<a name="line273"></a>    }
<a name="line274"></a>  }
<a name="line275"></a>};
<a name="line276"></a>
<a name="line277"></a>
<a name="line278"></a>/**
<a name="line279"></a> * Returns the index-th 32-bit (signed) piece of the Integer according to
<a name="line280"></a> * little-endian order (i.e., index 0 contains the smallest bits).
<a name="line281"></a> * @param {number} index The index in question.
<a name="line282"></a> * @return {number} The requested 32-bits as a signed number.
<a name="line283"></a> */
<a name="line284"></a>goog.math.Integer.prototype.getBits = function(index) {
<a name="line285"></a>  if (index &lt; 0) {
<a name="line286"></a>    return 0;  // Allowing this simplifies bit shifting operations below...
<a name="line287"></a>  } else if (index &lt; this.bits_.length) {
<a name="line288"></a>    return this.bits_[index];
<a name="line289"></a>  } else {
<a name="line290"></a>    return this.sign_;
<a name="line291"></a>  }
<a name="line292"></a>};
<a name="line293"></a>
<a name="line294"></a>
<a name="line295"></a>/**
<a name="line296"></a> * Returns the index-th 32-bit piece as an unsigned number.
<a name="line297"></a> * @param {number} index The index in question.
<a name="line298"></a> * @return {number} The requested 32-bits as an unsigned number.
<a name="line299"></a> */
<a name="line300"></a>goog.math.Integer.prototype.getBitsUnsigned = function(index) {
<a name="line301"></a>  var val = this.getBits(index);
<a name="line302"></a>  return val &gt;= 0 ? val : goog.math.Integer.TWO_PWR_32_DBL_ + val;
<a name="line303"></a>};
<a name="line304"></a>
<a name="line305"></a>
<a name="line306"></a>/** @return {number} The sign bit of this number, -1 or 0. */
<a name="line307"></a>goog.math.Integer.prototype.getSign = function() {
<a name="line308"></a>  return this.sign_;
<a name="line309"></a>};
<a name="line310"></a>
<a name="line311"></a>
<a name="line312"></a>/** @return {boolean} Whether this value is zero. */
<a name="line313"></a>goog.math.Integer.prototype.isZero = function() {
<a name="line314"></a>  if (this.sign_ != 0) {
<a name="line315"></a>    return false;
<a name="line316"></a>  }
<a name="line317"></a>  for (var i = 0; i &lt; this.bits_.length; i++) {
<a name="line318"></a>    if (this.bits_[i] != 0) {
<a name="line319"></a>      return false;
<a name="line320"></a>    }
<a name="line321"></a>  }
<a name="line322"></a>  return true;
<a name="line323"></a>};
<a name="line324"></a>
<a name="line325"></a>
<a name="line326"></a>/** @return {boolean} Whether this value is negative. */
<a name="line327"></a>goog.math.Integer.prototype.isNegative = function() {
<a name="line328"></a>  return this.sign_ == -1;
<a name="line329"></a>};
<a name="line330"></a>
<a name="line331"></a>
<a name="line332"></a>/** @return {boolean} Whether this value is odd. */
<a name="line333"></a>goog.math.Integer.prototype.isOdd = function() {
<a name="line334"></a>  return (this.bits_.length == 0) &amp;&amp; (this.sign_ == -1) ||
<a name="line335"></a>         (this.bits_.length &gt; 0) &amp;&amp; ((this.bits_[0] &amp; 1) != 0);
<a name="line336"></a>};
<a name="line337"></a>
<a name="line338"></a>
<a name="line339"></a>/**
<a name="line340"></a> * @param {goog.math.Integer} other Integer to compare against.
<a name="line341"></a> * @return {boolean} Whether this Integer equals the other.
<a name="line342"></a> */
<a name="line343"></a>goog.math.Integer.prototype.equals = function(other) {
<a name="line344"></a>  if (this.sign_ != other.sign_) {
<a name="line345"></a>    return false;
<a name="line346"></a>  }
<a name="line347"></a>  var len = Math.max(this.bits_.length, other.bits_.length);
<a name="line348"></a>  for (var i = 0; i &lt; len; i++) {
<a name="line349"></a>    if (this.getBits(i) != other.getBits(i)) {
<a name="line350"></a>      return false;
<a name="line351"></a>    }
<a name="line352"></a>  }
<a name="line353"></a>  return true;
<a name="line354"></a>};
<a name="line355"></a>
<a name="line356"></a>
<a name="line357"></a>/**
<a name="line358"></a> * @param {goog.math.Integer} other Integer to compare against.
<a name="line359"></a> * @return {boolean} Whether this Integer does not equal the other.
<a name="line360"></a> */
<a name="line361"></a>goog.math.Integer.prototype.notEquals = function(other) {
<a name="line362"></a>  return !this.equals(other);
<a name="line363"></a>};
<a name="line364"></a>
<a name="line365"></a>
<a name="line366"></a>/**
<a name="line367"></a> * @param {goog.math.Integer} other Integer to compare against.
<a name="line368"></a> * @return {boolean} Whether this Integer is greater than the other.
<a name="line369"></a> */
<a name="line370"></a>goog.math.Integer.prototype.greaterThan = function(other) {
<a name="line371"></a>  return this.compare(other) &gt; 0;
<a name="line372"></a>};
<a name="line373"></a>
<a name="line374"></a>
<a name="line375"></a>/**
<a name="line376"></a> * @param {goog.math.Integer} other Integer to compare against.
<a name="line377"></a> * @return {boolean} Whether this Integer is greater than or equal to the other.
<a name="line378"></a> */
<a name="line379"></a>goog.math.Integer.prototype.greaterThanOrEqual = function(other) {
<a name="line380"></a>  return this.compare(other) &gt;= 0;
<a name="line381"></a>};
<a name="line382"></a>
<a name="line383"></a>
<a name="line384"></a>/**
<a name="line385"></a> * @param {goog.math.Integer} other Integer to compare against.
<a name="line386"></a> * @return {boolean} Whether this Integer is less than the other.
<a name="line387"></a> */
<a name="line388"></a>goog.math.Integer.prototype.lessThan = function(other) {
<a name="line389"></a>  return this.compare(other) &lt; 0;
<a name="line390"></a>};
<a name="line391"></a>
<a name="line392"></a>
<a name="line393"></a>/**
<a name="line394"></a> * @param {goog.math.Integer} other Integer to compare against.
<a name="line395"></a> * @return {boolean} Whether this Integer is less than or equal to the other.
<a name="line396"></a> */
<a name="line397"></a>goog.math.Integer.prototype.lessThanOrEqual = function(other) {
<a name="line398"></a>  return this.compare(other) &lt;= 0;
<a name="line399"></a>};
<a name="line400"></a>
<a name="line401"></a>
<a name="line402"></a>/**
<a name="line403"></a> * Compares this Integer with the given one.
<a name="line404"></a> * @param {goog.math.Integer} other Integer to compare against.
<a name="line405"></a> * @return {number} 0 if they are the same, 1 if the this is greater, and -1
<a name="line406"></a> *     if the given one is greater.
<a name="line407"></a> */
<a name="line408"></a>goog.math.Integer.prototype.compare = function(other) {
<a name="line409"></a>  var diff = this.subtract(other);
<a name="line410"></a>  if (diff.isNegative()) {
<a name="line411"></a>    return -1;
<a name="line412"></a>  } else if (diff.isZero()) {
<a name="line413"></a>    return 0;
<a name="line414"></a>  } else {
<a name="line415"></a>    return +1;
<a name="line416"></a>  }
<a name="line417"></a>};
<a name="line418"></a>
<a name="line419"></a>
<a name="line420"></a>/**
<a name="line421"></a> * Returns an integer with only the first numBits bits of this value, sign
<a name="line422"></a> * extended from the final bit.
<a name="line423"></a> * @param {number} numBits The number of bits by which to shift.
<a name="line424"></a> * @return {!goog.math.Integer} The shorted integer value.
<a name="line425"></a> */
<a name="line426"></a>goog.math.Integer.prototype.shorten = function(numBits) {
<a name="line427"></a>  var arr_index = (numBits - 1) &gt;&gt; 5;
<a name="line428"></a>  var bit_index = (numBits - 1) % 32;
<a name="line429"></a>  var bits = [];
<a name="line430"></a>  for (var i = 0; i &lt; arr_index; i++) {
<a name="line431"></a>    bits[i] = this.getBits(i);
<a name="line432"></a>  }
<a name="line433"></a>  var sigBits = bit_index == 31 ? 0xFFFFFFFF : (1 &lt;&lt; (bit_index + 1)) - 1;
<a name="line434"></a>  var val = this.getBits(arr_index) &amp; sigBits;
<a name="line435"></a>  if (val &amp; (1 &lt;&lt; bit_index)) {
<a name="line436"></a>    val |= 0xFFFFFFFF - sigBits;
<a name="line437"></a>    bits[arr_index] = val;
<a name="line438"></a>    return new goog.math.Integer(bits, -1);
<a name="line439"></a>  } else {
<a name="line440"></a>    bits[arr_index] = val;
<a name="line441"></a>    return new goog.math.Integer(bits, 0);
<a name="line442"></a>  }
<a name="line443"></a>};
<a name="line444"></a>
<a name="line445"></a>
<a name="line446"></a>/** @return {!goog.math.Integer} The negation of this value. */
<a name="line447"></a>goog.math.Integer.prototype.negate = function() {
<a name="line448"></a>  return this.not().add(goog.math.Integer.ONE);
<a name="line449"></a>};
<a name="line450"></a>
<a name="line451"></a>
<a name="line452"></a>/**
<a name="line453"></a> * Returns the sum of this and the given Integer.
<a name="line454"></a> * @param {goog.math.Integer} other The Integer to add to this.
<a name="line455"></a> * @return {!goog.math.Integer} The Integer result.
<a name="line456"></a> */
<a name="line457"></a>goog.math.Integer.prototype.add = function(other) {
<a name="line458"></a>  var len = Math.max(this.bits_.length, other.bits_.length);
<a name="line459"></a>  var arr = [];
<a name="line460"></a>  var carry = 0;
<a name="line461"></a>
<a name="line462"></a>  for (var i = 0; i &lt;= len; i++) {
<a name="line463"></a>    var a1 = this.getBits(i) &gt;&gt;&gt; 16;
<a name="line464"></a>    var a0 = this.getBits(i) &amp; 0xFFFF;
<a name="line465"></a>
<a name="line466"></a>    var b1 = other.getBits(i) &gt;&gt;&gt; 16;
<a name="line467"></a>    var b0 = other.getBits(i) &amp; 0xFFFF;
<a name="line468"></a>
<a name="line469"></a>    var c0 = carry + a0 + b0;
<a name="line470"></a>    var c1 = (c0 &gt;&gt;&gt; 16) + a1 + b1;
<a name="line471"></a>    carry = c1 &gt;&gt;&gt; 16;
<a name="line472"></a>    c0 &amp;= 0xFFFF;
<a name="line473"></a>    c1 &amp;= 0xFFFF;
<a name="line474"></a>    arr[i] = (c1 &lt;&lt; 16) | c0;
<a name="line475"></a>  }
<a name="line476"></a>  return goog.math.Integer.fromBits(arr);
<a name="line477"></a>};
<a name="line478"></a>
<a name="line479"></a>
<a name="line480"></a>/**
<a name="line481"></a> * Returns the difference of this and the given Integer.
<a name="line482"></a> * @param {goog.math.Integer} other The Integer to subtract from this.
<a name="line483"></a> * @return {!goog.math.Integer} The Integer result.
<a name="line484"></a> */
<a name="line485"></a>goog.math.Integer.prototype.subtract = function(other) {
<a name="line486"></a>  return this.add(other.negate());
<a name="line487"></a>};
<a name="line488"></a>
<a name="line489"></a>
<a name="line490"></a>/**
<a name="line491"></a> * Returns the product of this and the given Integer.
<a name="line492"></a> * @param {goog.math.Integer} other The Integer to multiply against this.
<a name="line493"></a> * @return {!goog.math.Integer} The product of this and the other.
<a name="line494"></a> */
<a name="line495"></a>goog.math.Integer.prototype.multiply = function(other) {
<a name="line496"></a>  if (this.isZero()) {
<a name="line497"></a>    return goog.math.Integer.ZERO;
<a name="line498"></a>  } else if (other.isZero()) {
<a name="line499"></a>    return goog.math.Integer.ZERO;
<a name="line500"></a>  }
<a name="line501"></a>
<a name="line502"></a>  if (this.isNegative()) {
<a name="line503"></a>    if (other.isNegative()) {
<a name="line504"></a>      return this.negate().multiply(other.negate());
<a name="line505"></a>    } else {
<a name="line506"></a>      return this.negate().multiply(other).negate();
<a name="line507"></a>    }
<a name="line508"></a>  } else if (other.isNegative()) {
<a name="line509"></a>    return this.multiply(other.negate()).negate();
<a name="line510"></a>  }
<a name="line511"></a>
<a name="line512"></a>  // If both numbers are small, use float multiplication
<a name="line513"></a>  if (this.lessThan(goog.math.Integer.TWO_PWR_24_) &amp;&amp;
<a name="line514"></a>      other.lessThan(goog.math.Integer.TWO_PWR_24_)) {
<a name="line515"></a>    return goog.math.Integer.fromNumber(this.toNumber() * other.toNumber());
<a name="line516"></a>  }
<a name="line517"></a>
<a name="line518"></a>  // Fill in an array of 16-bit products.
<a name="line519"></a>  var len = this.bits_.length + other.bits_.length;
<a name="line520"></a>  var arr = [];
<a name="line521"></a>  for (var i = 0; i &lt; 2 * len; i++) {
<a name="line522"></a>    arr[i] = 0;
<a name="line523"></a>  }
<a name="line524"></a>  for (var i = 0; i &lt; this.bits_.length; i++) {
<a name="line525"></a>    for (var j = 0; j &lt; other.bits_.length; j++) {
<a name="line526"></a>      var a1 = this.getBits(i) &gt;&gt;&gt; 16;
<a name="line527"></a>      var a0 = this.getBits(i) &amp; 0xFFFF;
<a name="line528"></a>
<a name="line529"></a>      var b1 = other.getBits(j) &gt;&gt;&gt; 16;
<a name="line530"></a>      var b0 = other.getBits(j) &amp; 0xFFFF;
<a name="line531"></a>
<a name="line532"></a>      arr[2 * i + 2 * j] += a0 * b0;
<a name="line533"></a>      goog.math.Integer.carry16_(arr, 2 * i + 2 * j);
<a name="line534"></a>      arr[2 * i + 2 * j + 1] += a1 * b0;
<a name="line535"></a>      goog.math.Integer.carry16_(arr, 2 * i + 2 * j + 1);
<a name="line536"></a>      arr[2 * i + 2 * j + 1] += a0 * b1;
<a name="line537"></a>      goog.math.Integer.carry16_(arr, 2 * i + 2 * j + 1);
<a name="line538"></a>      arr[2 * i + 2 * j + 2] += a1 * b1;
<a name="line539"></a>      goog.math.Integer.carry16_(arr, 2 * i + 2 * j + 2);
<a name="line540"></a>    }
<a name="line541"></a>  }
<a name="line542"></a>
<a name="line543"></a>  // Combine the 16-bit values into 32-bit values.
<a name="line544"></a>  for (var i = 0; i &lt; len; i++) {
<a name="line545"></a>    arr[i] = (arr[2 * i + 1] &lt;&lt; 16) | arr[2 * i];
<a name="line546"></a>  }
<a name="line547"></a>  for (var i = len; i &lt; 2 * len; i++) {
<a name="line548"></a>    arr[i] = 0;
<a name="line549"></a>  }
<a name="line550"></a>  return new goog.math.Integer(arr, 0);
<a name="line551"></a>};
<a name="line552"></a>
<a name="line553"></a>
<a name="line554"></a>/**
<a name="line555"></a> * Carries any overflow from the given index into later entries.
<a name="line556"></a> * @param {Array.&lt;number&gt;} bits Array of 16-bit values in little-endian order.
<a name="line557"></a> * @param {number} index The index in question.
<a name="line558"></a> * @private
<a name="line559"></a> */
<a name="line560"></a>goog.math.Integer.carry16_ = function(bits, index) {
<a name="line561"></a>  while ((bits[index] &amp; 0xFFFF) != bits[index]) {
<a name="line562"></a>    bits[index + 1] += bits[index] &gt;&gt;&gt; 16;
<a name="line563"></a>    bits[index] &amp;= 0xFFFF;
<a name="line564"></a>  }
<a name="line565"></a>};
<a name="line566"></a>
<a name="line567"></a>
<a name="line568"></a>/**
<a name="line569"></a> * Returns this Integer divided by the given one.
<a name="line570"></a> * @param {goog.math.Integer} other Th Integer to divide this by.
<a name="line571"></a> * @return {!goog.math.Integer} This value divided by the given one.
<a name="line572"></a> */
<a name="line573"></a>goog.math.Integer.prototype.divide = function(other) {
<a name="line574"></a>  if (other.isZero()) {
<a name="line575"></a>    throw Error(&#39;division by zero&#39;);
<a name="line576"></a>  } else if (this.isZero()) {
<a name="line577"></a>    return goog.math.Integer.ZERO;
<a name="line578"></a>  }
<a name="line579"></a>
<a name="line580"></a>  if (this.isNegative()) {
<a name="line581"></a>    if (other.isNegative()) {
<a name="line582"></a>      return this.negate().divide(other.negate());
<a name="line583"></a>    } else {
<a name="line584"></a>      return this.negate().divide(other).negate();
<a name="line585"></a>    }
<a name="line586"></a>  } else if (other.isNegative()) {
<a name="line587"></a>    return this.divide(other.negate()).negate();
<a name="line588"></a>  }
<a name="line589"></a>
<a name="line590"></a>  // Repeat the following until the remainder is less than other:  find a
<a name="line591"></a>  // floating-point that approximates remainder / other *from below*, add this
<a name="line592"></a>  // into the result, and subtract it from the remainder.  It is critical that
<a name="line593"></a>  // the approximate value is less than or equal to the real value so that the
<a name="line594"></a>  // remainder never becomes negative.
<a name="line595"></a>  var res = goog.math.Integer.ZERO;
<a name="line596"></a>  var rem = this;
<a name="line597"></a>  while (rem.greaterThanOrEqual(other)) {
<a name="line598"></a>    // Approximate the result of division. This may be a little greater or
<a name="line599"></a>    // smaller than the actual value.
<a name="line600"></a>    var approx = Math.max(1, Math.floor(rem.toNumber() / other.toNumber()));
<a name="line601"></a>
<a name="line602"></a>    // We will tweak the approximate result by changing it in the 48-th digit or
<a name="line603"></a>    // the smallest non-fractional digit, whichever is larger.
<a name="line604"></a>    var log2 = Math.ceil(Math.log(approx) / Math.LN2);
<a name="line605"></a>    var delta = (log2 &lt;= 48) ? 1 : Math.pow(2, log2 - 48);
<a name="line606"></a>
<a name="line607"></a>    // Decrease the approximation until it is smaller than the remainder.  Note
<a name="line608"></a>    // that if it is too large, the product overflows and is negative.
<a name="line609"></a>    var approxRes = goog.math.Integer.fromNumber(approx);
<a name="line610"></a>    var approxRem = approxRes.multiply(other);
<a name="line611"></a>    while (approxRem.isNegative() || approxRem.greaterThan(rem)) {
<a name="line612"></a>      approx -= delta;
<a name="line613"></a>      approxRes = goog.math.Integer.fromNumber(approx);
<a name="line614"></a>      approxRem = approxRes.multiply(other);
<a name="line615"></a>    }
<a name="line616"></a>
<a name="line617"></a>    // We know the answer can&#39;t be zero... and actually, zero would cause
<a name="line618"></a>    // infinite recursion since we would make no progress.
<a name="line619"></a>    if (approxRes.isZero()) {
<a name="line620"></a>      approxRes = goog.math.Integer.ONE;
<a name="line621"></a>    }
<a name="line622"></a>
<a name="line623"></a>    res = res.add(approxRes);
<a name="line624"></a>    rem = rem.subtract(approxRem);
<a name="line625"></a>  }
<a name="line626"></a>  return res;
<a name="line627"></a>};
<a name="line628"></a>
<a name="line629"></a>
<a name="line630"></a>/**
<a name="line631"></a> * Returns this Integer modulo the given one.
<a name="line632"></a> * @param {goog.math.Integer} other The Integer by which to mod.
<a name="line633"></a> * @return {!goog.math.Integer} This value modulo the given one.
<a name="line634"></a> */
<a name="line635"></a>goog.math.Integer.prototype.modulo = function(other) {
<a name="line636"></a>  return this.subtract(this.divide(other).multiply(other));
<a name="line637"></a>};
<a name="line638"></a>
<a name="line639"></a>
<a name="line640"></a>/** @return {!goog.math.Integer} The bitwise-NOT of this value. */
<a name="line641"></a>goog.math.Integer.prototype.not = function() {
<a name="line642"></a>  var len = this.bits_.length;
<a name="line643"></a>  var arr = [];
<a name="line644"></a>  for (var i = 0; i &lt; len; i++) {
<a name="line645"></a>    arr[i] = ~this.bits_[i];
<a name="line646"></a>  }
<a name="line647"></a>  return new goog.math.Integer(arr, ~this.sign_);
<a name="line648"></a>};
<a name="line649"></a>
<a name="line650"></a>
<a name="line651"></a>/**
<a name="line652"></a> * Returns the bitwise-AND of this Integer and the given one.
<a name="line653"></a> * @param {goog.math.Integer} other The Integer to AND with this.
<a name="line654"></a> * @return {!goog.math.Integer} The bitwise-AND of this and the other.
<a name="line655"></a> */
<a name="line656"></a>goog.math.Integer.prototype.and = function(other) {
<a name="line657"></a>  var len = Math.max(this.bits_.length, other.bits_.length);
<a name="line658"></a>  var arr = [];
<a name="line659"></a>  for (var i = 0; i &lt; len; i++) {
<a name="line660"></a>    arr[i] = this.getBits(i) &amp; other.getBits(i);
<a name="line661"></a>  }
<a name="line662"></a>  return new goog.math.Integer(arr, this.sign_ &amp; other.sign_);
<a name="line663"></a>};
<a name="line664"></a>
<a name="line665"></a>
<a name="line666"></a>/**
<a name="line667"></a> * Returns the bitwise-OR of this Integer and the given one.
<a name="line668"></a> * @param {goog.math.Integer} other The Integer to OR with this.
<a name="line669"></a> * @return {!goog.math.Integer} The bitwise-OR of this and the other.
<a name="line670"></a> */
<a name="line671"></a>goog.math.Integer.prototype.or = function(other) {
<a name="line672"></a>  var len = Math.max(this.bits_.length, other.bits_.length);
<a name="line673"></a>  var arr = [];
<a name="line674"></a>  for (var i = 0; i &lt; len; i++) {
<a name="line675"></a>    arr[i] = this.getBits(i) | other.getBits(i);
<a name="line676"></a>  }
<a name="line677"></a>  return new goog.math.Integer(arr, this.sign_ | other.sign_);
<a name="line678"></a>};
<a name="line679"></a>
<a name="line680"></a>
<a name="line681"></a>/**
<a name="line682"></a> * Returns the bitwise-XOR of this Integer and the given one.
<a name="line683"></a> * @param {goog.math.Integer} other The Integer to XOR with this.
<a name="line684"></a> * @return {!goog.math.Integer} The bitwise-XOR of this and the other.
<a name="line685"></a> */
<a name="line686"></a>goog.math.Integer.prototype.xor = function(other) {
<a name="line687"></a>  var len = Math.max(this.bits_.length, other.bits_.length);
<a name="line688"></a>  var arr = [];
<a name="line689"></a>  for (var i = 0; i &lt; len; i++) {
<a name="line690"></a>    arr[i] = this.getBits(i) ^ other.getBits(i);
<a name="line691"></a>  }
<a name="line692"></a>  return new goog.math.Integer(arr, this.sign_ ^ other.sign_);
<a name="line693"></a>};
<a name="line694"></a>
<a name="line695"></a>
<a name="line696"></a>/**
<a name="line697"></a> * Returns this value with bits shifted to the left by the given amount.
<a name="line698"></a> * @param {number} numBits The number of bits by which to shift.
<a name="line699"></a> * @return {!goog.math.Integer} This shifted to the left by the given amount.
<a name="line700"></a> */
<a name="line701"></a>goog.math.Integer.prototype.shiftLeft = function(numBits) {
<a name="line702"></a>  var arr_delta = numBits &gt;&gt; 5;
<a name="line703"></a>  var bit_delta = numBits % 32;
<a name="line704"></a>  var len = this.bits_.length + arr_delta + (bit_delta &gt; 0 ? 1 : 0);
<a name="line705"></a>  var arr = [];
<a name="line706"></a>  for (var i = 0; i &lt; len; i++) {
<a name="line707"></a>    if (bit_delta &gt; 0) {
<a name="line708"></a>      arr[i] = (this.getBits(i - arr_delta) &lt;&lt; bit_delta) |
<a name="line709"></a>               (this.getBits(i - arr_delta - 1) &gt;&gt;&gt; (32 - bit_delta));
<a name="line710"></a>    } else {
<a name="line711"></a>      arr[i] = this.getBits(i - arr_delta);
<a name="line712"></a>    }
<a name="line713"></a>  }
<a name="line714"></a>  return new goog.math.Integer(arr, this.sign_);
<a name="line715"></a>};
<a name="line716"></a>
<a name="line717"></a>
<a name="line718"></a>/**
<a name="line719"></a> * Returns this value with bits shifted to the right by the given amount.
<a name="line720"></a> * @param {number} numBits The number of bits by which to shift.
<a name="line721"></a> * @return {!goog.math.Integer} This shifted to the right by the given amount.
<a name="line722"></a> */
<a name="line723"></a>goog.math.Integer.prototype.shiftRight = function(numBits) {
<a name="line724"></a>  var arr_delta = numBits &gt;&gt; 5;
<a name="line725"></a>  var bit_delta = numBits % 32;
<a name="line726"></a>  var len = this.bits_.length - arr_delta;
<a name="line727"></a>  var arr = [];
<a name="line728"></a>  for (var i = 0; i &lt; len; i++) {
<a name="line729"></a>    if (bit_delta &gt; 0) {
<a name="line730"></a>      arr[i] = (this.getBits(i + arr_delta) &gt;&gt;&gt; bit_delta) |
<a name="line731"></a>               (this.getBits(i + arr_delta + 1) &lt;&lt; (32 - bit_delta));
<a name="line732"></a>    } else {
<a name="line733"></a>      arr[i] = this.getBits(i + arr_delta);
<a name="line734"></a>    }
<a name="line735"></a>  }
<a name="line736"></a>  return new goog.math.Integer(arr, this.sign_);
<a name="line737"></a>};
</pre>


</body>
</html>
